#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Net;
using pds2.XArena;
#endregion

namespace QuizArena
{
    /// <summary>
    /// The pause menu comes up over the top of the game,
    /// giving the player options to resume or quit.
    /// </summary>
    class TelaDePause : TelaDeMenu
    {
        #region Fields

        NetworkSession networkSession;
        
        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public TelaDePause(NetworkSession networkSession)
            : base(Resources.Pausado)
        {
            this.networkSession = networkSession;

            // Flag that there is no need for the game to transition
            // off when the pause menu is on top of it.
            IsPopup = true;

            // Add the Resume Game menu entry.
            ItemDeMenu resumeGameMenuEntry = new ItemDeMenu(Resources.VoltarParaJogo);
            resumeGameMenuEntry.Selecionado += Sair;
            ItensDeMenu.Add(resumeGameMenuEntry);

            if (networkSession == null)
            {
                // If this is a single player game, add the Quit menu entry.
                ItemDeMenu quitGameMenuEntry = new ItemDeMenu(Resources.SairDoJogo);
                quitGameMenuEntry.Selecionado += QuitGameMenuEntrySelected;
                ItensDeMenu.Add(quitGameMenuEntry);
            }
            else
            {
                // If we are hosting a network game, add the Return to Lobby menu entry.
                if (networkSession.IsHost)
                {
                    ItemDeMenu lobbyMenuEntry = new ItemDeMenu(Resources.VoltarParaSala);
                    lobbyMenuEntry.Selecionado += ReturnToLobbyMenuEntrySelected;
                    ItensDeMenu.Add(lobbyMenuEntry);
                }

                // Add the End/Leave Session menu entry.
                string leaveEntryText = networkSession.IsHost ? Resources.EndSession :
                                                                Resources.LeaveSession;

                ItemDeMenu leaveSessionMenuEntry = new ItemDeMenu(leaveEntryText);
                leaveSessionMenuEntry.Selecionado += LeaveSessionMenuEntrySelected;
                ItensDeMenu.Add(leaveSessionMenuEntry);
            }

            PosicaoTitulo = new Vector2(PosicaoTitulo.X, PosicaoTitulo.Y - 50);
        }


        #endregion

        #region Handle Input


        /// <summary>
        /// Event handler for when the Quit Game menu entry is selected.
        /// </summary>
        void QuitGameMenuEntrySelected(object sender, EventArgs e)
        {
            CaixaDeMensagem confirmQuitMessageBox =
                                    new CaixaDeMensagem(Resources.ConfirmQuitGame);

            confirmQuitMessageBox.Accepted += ConfirmQuitMessageBoxAccepted;

            GerenciadorDeTelas.AdicionarTela(confirmQuitMessageBox);
        }


        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to quit" message box. This uses the loading screen to
        /// transition from the game back to the main menu screen.
        /// </summary>
        void ConfirmQuitMessageBoxAccepted(object sender, EventArgs e)
        {
            TelaDeCarregamento.Load(GerenciadorDeTelas, false, new TelaDeFundo(),
                                                     new TelaDoMenuPrincipal());
        }


        /// <summary>
        /// Event handler for when the Return to Lobby menu entry is selected.
        /// </summary>
        void ReturnToLobbyMenuEntrySelected(object sender, EventArgs e)
        {
            if (networkSession.SessionState == NetworkSessionState.Playing)
            {
                networkSession.EndGame();

                TelaDeCarregamento.Load(GerenciadorDeTelas, false, new TelaDoServidor(networkSession));
            }

        }


        /// <summary>
        /// Event handler for when the End/Leave Session menu entry is selected.
        /// </summary>
        void LeaveSessionMenuEntrySelected(object sender, EventArgs e)
        {
            ComponenteDeSessaoDeRede.LeaveSession(GerenciadorDeTelas);
        }


        #endregion

        #region Draw


        /// <summary>
        /// Draws the pause menu screen. This darkens down the gameplay screen
        /// that is underneath us, and then chains to the base MenuScreen.Draw.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            GerenciadorDeTelas.FadeBackBufferToBlack(TransicaoAlpha * 2 / 3);

            base.Draw(gameTime);
        }


        #endregion
    }
}
